// 文章管理  API
var express = require('express');
var router = express.Router();
const app = express()
const cors = require('cors');
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// 日志：
const computingLog = require('../../middlewares/computingLog')
const CategorieModel = require('../../Models/CategoriesModel');
const SQModel = require('../../Models/SQModel');
// 获取数据库数据
router.get('/Categorie', computingLog, function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');

  CategorieModel.find().sort({ time: -1 }).then((data1) => {
    // 先初始化count为0
    data1.forEach(item => { item.count = 0 });

    SQModel.find().sort({ time: -1 }).then((data2) => {
      // 统计
      for (let i = 0; i < data1.length; i++) {
        for (let j = 0; j < data2.length; j++) {
          if (data2[j].type === data1[i].name) {
            data1[i].count += 1;
          }
        }
      }
      res.json({
        code: '0000',
        msg: '获取成功',
        data: data1
      });
    }).catch((err) => {
      res.json({
        code: '1002',
        msg: '获取文章失败',
        data: []
      });
    })
  }).catch((err) => {
    res.json({
      code: '1001',
      msg: '获取分类失败',
      data: []
    });
  });
});

// // 删除一条数据库数据
router.delete('/Categorie/:id', computingLog, function (req, res) {
  res.header('Access-Control-Allow-Origin', '*'); // 或指定为 'http://localhost:8080'
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  let id = req.params.id
  CategorieModel.deleteOne({ _id: id }).then((data) => {
    res.json({
      code: '0000',
      msg: '删除成功',
      data: ''
    })
  }).catch((err) => {
    res.json({
      code: '1002',
      msg: '删除失败',
      data: null
    })
    return
  })
})

// // 添加数据库数据
router.post('/AddCategorie', computingLog, function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*'); // 或指定为 'http://localhost:8080'
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  // 获取数据库数据
  // console.log("收到数据：", req.body);
  CategorieModel.create({ ...req.body }).then((data) => {
    res.json({
      code: '0000',
      msg: '添加成功',
      data: data
    })
  }).catch((err) => {
    res.json({
      code: '1003',
      msg: '添加失败',
      data: null
    })
    return
  })
})

// // 编辑更新单条数据
router.patch('/Categorie/:id', computingLog, (req, res) => {
  res.header('Access-Control-Allow-Origin', '*'); // 或指定为 'http://localhost:8080'
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  let id = req.params.id
  CategorieModel.updateOne({ _id: id }, req.body).then((data) => {
    CategorieModel.findById(id).then((data) => {
      res.json({
        code: "0000",
        msg: '更新成功了',
        data: data
      })
    }).catch((err) => {
      res.json({
        code: "1005",
        msg: "更新失败",
        data: null
      })
    })
  })

})

module.exports = router;